﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using RBS.Common;
using RBS.Core.Model;
using RBS.Web.API.Models;

namespace RBS.Web.API.Areas.User.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class IntegrationController : ApiController
    {
        private ApiContext db = new ApiContext();
         
        // GET api/Integration/5
        /// <summary>
        /// 获取用户积分列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isCount"></param>
        /// <returns></returns>
        public JsonData GetU_User_Integration(int id,bool isCount=false)
        {  
            var jsonData = new JsonData { Success = true };
            try
            {
                if (isCount)
                {
                    var item = db.U_User_Integration.Where(c => c.UserId == id).Sum(c => c.Point); 
                    jsonData.Data = item;
                }
                else
                {
                    var item = db.U_User_Integration.Where(c => c.UserId == id);
                    jsonData.Data = item;
                }  
            }
            catch (Exception exception)
            {
                jsonData.Error = exception.Message.ToString();
                jsonData.Success = false;
            }
            return jsonData;
        } 

         /// <summary>
         /// 赠与积分操作
         /// </summary>
         /// <param name="model"></param>
         /// <returns></returns>
        public JsonData GiftIntegration([FromBody] IntegrationModel model)
        {
            var jsonData = new JsonData { Success = true };
            try
            {
                var GetIntegral = new RBS.Core.Model.U_User_Integration
                {
                    UserId = model.FriendId,
                    Point = model.Point,
                    PointTime = DateTime.Now,
                    HotelId = model.UserId,
                    Type = (int)RBSEnums.IntegraType.好友赠送
                };
                var GiftIntegral = new RBS.Core.Model.U_User_Integration
                {
                    UserId = model.UserId,
                    Point = -model.Point,
                    PointTime = DateTime.Now,
                    HotelId = model.FriendId,
                    Type = (int)RBSEnums.IntegraType.赠送积分
                };
                var bll = new RBS.Core.BLL.U_User_Integration();
                bll.Add(GetIntegral);
                bll.Add(GiftIntegral); 
                 
            }
            catch (Exception exception)
            {
                jsonData.Error = exception.Message.ToString();
                jsonData.Success = false;
            }
            return jsonData;
        }
        
        
/*
        // PUT api/Integration/5
        public HttpResponseMessage PutU_User_Integration(int id, U_User_Integration u_user_integration)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != u_user_integration.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(u_user_integration).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Integration
        public HttpResponseMessage PostU_User_Integration(V_UserIntegration u_user_integration)
        {
            if (ModelState.IsValid)
            {
                db.U_User_Integration.Add(u_user_integration);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, u_user_integration);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = u_user_integration.Id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/Integration/5
        public HttpResponseMessage DeleteU_User_Integration(int id)
        {
            V_UserIntegration u_user_integration = db.U_User_Integration.Find(id);
            if (u_user_integration == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.U_User_Integration.Remove(u_user_integration);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, u_user_integration);
        }*/

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

    /// <summary>
    ///赠与积分实体
    /// </summary>
    public class IntegrationModel
    {
        //int UserId, int Point,int FriendId
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get; set; }
        /// <summary>
        /// 积分 （正数为新增积分，负数为减积分）
        /// </summary>
        public int Point { get; set; }
        /// <summary>
        /// 好友ID
        /// </summary>
        public int FriendId { get; set; }
    }
}